                                     
package com.selenium.library;

import java.net.URL;
import java.util.List;
import java.util.Properties;
import static junit.framework.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Platform;
import org.openqa.selenium.SeleneseCommandExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/* Author - Payal Garg
TestName - Main Library file
Date created - 29-04-2013
Date last modified - 29-05-2013 (by Payal Garg)
Reason for modification - added new functions*/

public class Driver {

 	static WebDriver wd;
 	String bro;
 	static Properties properties = new Properties();
 	static Properties props = new Properties();

	/* Author - Payal Garg
	Function - It maximizes the browser window
	Date created - 29-04-2013*/
	public void maximise()throws Exception{
 		 wd.manage().window().maximize();
 	 }
	
	
	
	
	/* Author - Payal Garg
	Function - it will take the control back to homepage
	Arguments - homepage argument
	Date created - 29-04-2013*/
	public void homePage(String homePage){
		try{
			if(homePage.equalsIgnoreCase("homepage")){
				wd.findElement(By.linkText("Home")).click();
			}
		}catch(Exception err){

			err.printStackTrace();
			System.out.println("Not on homepage, so going to HOMEPAGE");
			wd.get("http://www.appliancesonline.com.au");
		}
	}
		
	
	
	
	/* Author - Payal Garg
	Function - This function opens URL in desired webdriver
	Arguments - URL
	Date created - 29-04-2013 */
	public void open(String URL, String browser) throws Exception{
 
		try{
			try{
				if(wd != null){
					wd.quit();
				}	
			}catch(Exception err){
			}
//			URL url = new URL(properties.getProperty("serverURL"));
			DesiredCapabilities capabilities = new DesiredCapabilities();
			capabilities.setJavascriptEnabled(true);
			capabilities.setBrowserName(browser);
			capabilities.setPlatform(Platform.ANY);
			
			if(browser.equalsIgnoreCase("chrome")){
				wd = new RemoteWebDriver(new URL("http://127.0.0.1:9515/wd/hub"),DesiredCapabilities.chrome());
			}else{
			
				wd = new RemoteWebDriver(new URL("http://127.0.0.1:1236/wd/hub"),capabilities);
			}
			
			wd.navigate().to(URL);
			sync();
		 }catch(Exception err){
 			err.printStackTrace();
			wd.quit();
			fail("Driver couldn't invoke");
		 	}	
		}
	
	
	/* Author - Payal Garg
	Function - This function Sync between 2 web pages
	Arguments - none
	Date created - 29-04-2013 */
	public void sync() throws Exception{
		 try{
 			List<WebElement> myDynamicElement = (new WebDriverWait(wd,300)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));
			if(myDynamicElement.equals(null)){
				Thread.sleep(7000);
			}else{
				Thread.sleep(5000);
			}
		}catch(NoSuchElementException err){
			err.printStackTrace();
			fail("Element does not exists on AUT");
		 }catch(TimeoutException err){
 			err.printStackTrace();
			fail("Element does not exists with in 30s");
		 }
 	}
 	
 	
	
 	/* Author - Payal Garg
	Function - This function enters text in textbox
	Arguments - element xpath, text to be enter
	Date created - 29-04-2013 */
 	public void type(String element, String etext){
 			wd.findElement(By.xpath(element)).clear();
			wd.findElement(By.xpath(element)).sendKeys(etext);
  	}
	
	
	
	/* Author - Payal Garg
	Function - This function clicks on desired element
	Arguments - element xpath
	Date created - 29-04-2013 */
 
	public void click(String element) throws Exception{
 			sync();
	 		wd.findElement(By.xpath(element)).click();
			sync();
	 }
	
	
	
	/* Author - Payal Garg
	Function - This function gets text from desired element on web page
	Arguments - element xpath
	Date created - 29-04-2013 */
 	public String getText(String element){
		String text = null;
		text = wd.findElement(By.xpath(element)).getText();
		return text;
 	}
	
 	
	
	/* Author - Payal Garg
	Function - This function gets attribute value from desired element on web page
	Arguments - element xpath
	Date created - 29-04-2013 */
 	public String getAttribute(String element){
 		String getAttribute = null;
 		getAttribute = wd.findElement(By.xpath(element)).getAttribute("class");
  		return getAttribute;
 	}
	
	
 	
	/* Author - Payal Garg
	Function - This function selects value from the list
	Arguments - element xpath, text to select
	Date created - 29-04-2013 */
	public void select_list(String element, String etext) throws Exception{
			List <WebElement> myL = wd.findElements(By.xpath(element));
 			for (WebElement lItem : myL) {
				if (etext.equals(lItem.getText())){
					lItem.click();
				 }
 			}
	}
	
	
	
	
	/* Author - Payal Garg
	Function - This function clicks link
	Arguments - link name
	Date created - 29-04-2013 */
	public void linkClick(String linkname) throws Exception{
		wd.findElement(By.linkText(linkname)).click();
		
	//	wd.findElement(By.linkText(props.getProperty(linkname))).click();
 
	 }
	
	
	
	/* Author - Payal Garg
	Function - This function selects AJAX elements
	Arguments - parent element xpath, child element xpath, list of elements
	Date created - 29-04-2013 */
 	public void ajaxElementSelect(String parentelement, String childele, List<WebElement> element, String value) throws Exception{
			wd.findElement(By.xpath(parentelement)).sendKeys(" ");
			String city = value;
			for(int i = 1; i <=element.size(); i++){
				String childpath = childele + "[" + i + "]";
				if(city.equals(wd.findElement(By.xpath(childpath)).getText())){
					WebElement menu = wd.findElement(By.xpath(childpath));
	//				WebElement parentmenu = wd.findElement(By.xpath(props.getProperty(parentelement)));
					menu.click();
					sync();
					break;
				}
			}
		}
	
	
 	
	
	/* Author - Payal Garg
	Function - This function closes all the opened browsers
	Arguments - none
	Date created - 29-04-2013 */
	public void closeOpenedDriver(){ 
			wd.quit();
		
	}
	
	
	
	
	/* Author - Payal Garg
	Function - This function search for the product
	Arguments - element(search textbox) xpath, product to be searched for, search button xpath
	Date created - 29-04-2013 */
	public void search(String element,String product,String searchButton) throws Exception{
		type(element, product);
 		click(searchButton);
		sync();
 	}	
	
	
	
	
	
////	Author - Payal Garg
////	Function - This function search for the product
////	Arguments - None
////	Date created - 29-04-2013 
//	public void browseProduct() throws Exception{
//		try{
//			//Clicking on the next arrow to click
//			for (int j=1; j<3; j++)
//			 {
//	 			click("Search_Results_Next_Page_Link_Arrow_Footer");
//				sync();
//			 }
//	 
//			//Clicking on the previous arrow to click
//			int PageLink = (int) (1 + (Math.random() * (3 - 1)));
//			for (int j=3; j > PageLink; j--)
//	 		{
//				 click("Search_Results_Next_Page_Link_Arrow_Header");
//				 sync();
//	 		}
//		}catch(Exception err){
//			fail("Couldn't able to browse through product pages");
//		}
//	}
//
//	
	
	
	
//	Author - Payal Garg
//	Function - This function view product
//	Date created - 29-04-2013 
//	public void viewProduct() throws Exception {
//		try{
//					String parent = null;
//					do{
//						parent = getAttribute("view_product_detail_button");
//			 			if(!parent.equals("sli_list_result not-available")){
//							click("view_product_detail_button");
//			 				sync();
//						 }			
//			 			} while (parent.equals("sli_list_result not-available"));	
//			 
//			}catch(Exception err){
//				fail("cannot view the product page");
//			}
//		}
	
	
	
	
	
//	Author - Payal Garg
//	Function - This function add product
//	Date created - 29-04-2013 
// 	public void addCart() throws Exception {
// 		try{
//			click("add_to_cart_button");
//		}catch(Exception err){
//			fail("Product couldn't be added to cart");
//		}
//	 }
	
 	
 	
 	
// 	Author - Payal Garg
//	Function - checkout functionality
//	Date created - 29-04-2013 
// 	public void checkOut() throws Exception {
//		try{
//			String zip = props.getProperty("checkout_zipcode");		
//			type("Checkout_Zip_Code", zip);
//			sync();
//			wd.findElement(By.xpath(props.getProperty("Checkout_Zip_Code"))).sendKeys(" ");
//			List<WebElement> myele = wd.findElements(By.xpath(props.getProperty("AJAX_zip_code")));
//			ajaxElementSelect("Checkout_Zip_Code","AJAX_zip_code", myele, props.getProperty("list_value"));
//			click("check_out_button");
//			sync();
//			linkClick("Shipping_clear_form");
//		}catch(Exception err){
//		 fail("Couldn't get to checkout page");
// 
//		}
// 	}

}